#!/bin/bash  
# 通用版，自动检测或手动指定代理服务器IP和端口  

# ===【手动指定优先】===  
hostip="${PROXY_HOST:-127.0.0.1}"  
port="${PROXY_PORT:-7890}"  
# port="${PROXY_PORT:-7897}"  

# ===【如需自动检测主机IP（如容器或WSL等特殊用法）可启用】===  
# hostip=$(ip route show | grep -i default | awk '{print $3}')  
# hostip=$(hostname -I | awk '{print $1}')  # 本机VIP  

PROXY_HTTP="http://${hostip}:${port}"  

set_proxy(){  
    export http_proxy="${PROXY_HTTP}"  
    export HTTP_PROXY="${PROXY_HTTP}"  
    export https_proxy="${PROXY_HTTP}"  
    export HTTPS_PROXY="${PROXY_HTTP}"  
    git config --global http.proxy  "${PROXY_HTTP}"  
    git config --global https.proxy "${PROXY_HTTP}"  
}  

unset_proxy(){  
    unset http_proxy  
    unset HTTP_PROXY  
    unset https_proxy  
    unset HTTPS_PROXY  
    git config --global --unset http.proxy  
    git config --global --unset https.proxy  
}  

test_setting(){  
    echo "Host ip:" ${hostip}  
    echo "Current proxy:" "$https_proxy"  
    curl cip.cc  
}  

case "$1" in  
    set) set_proxy ;;  
    unset) unset_proxy ;;  
    test) test_setting ;;  
    *)  
        echo "Usage: $0 {set|unset|test}"  
        ;;  
esac
